package com.rural.bus.driver;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rural.bus.driver.config.MyPreferenceManager;
import com.rural.bus.driver.config.PreferenceKey;
import com.rural.bus.driver.model.DriverInfo;
import com.rural.bus.driver.net.MyHttpUtil;
import com.rural.bus.driver.net.OnNetCallback;
import com.rural.bus.driver.net.Url;
import com.rural.bus.driver.utils.MobileUtil;
import com.rural.bus.driver.utils.ProgressUtil;

public class LoginActivity extends BaseActivity {
	private EditText etPhoneNum, etUserpsw;
	private Button signin;
	private TextView tvForgetPsw;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		initView();
		setListeners();
		
		initData();
	}

    protected void setListeners(){
    	   signin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String phonenum = etPhoneNum.getText().toString();
				String psw = etUserpsw.getText().toString();
				
				boolean isMoblieLogic=MobileUtil.isMobileNO(phonenum);
				if(!isMoblieLogic){
					toast("手机号非法，请输入正确的手机号");
					return;
				}
				login(phonenum, psw);
			}
		});
    }
    
	@Override
	protected void initView() {
		signin = (Button) findViewById(R.id.signin);
		etPhoneNum = (EditText) findViewById(R.id.et_phonenum);
		etUserpsw = (EditText) findViewById(R.id.userpsw);
//		btQQlogin=(Button) findViewById(R.id.bt_qqlogin);
		
//		tvForgetPsw=(TextView) findViewById(R.id.tv_forget_psw);
//		tvForgetPsw.setText(Html.fromHtml("<u><font color=\"#023cfa\">忘记密码？</font></u>"));

		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setText("登录");
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));
	}

	@Override
	protected void initData() {

	}
	
	/**
	 * 登陆用户
	 */
	private void login(String phonenum,String psw) {
		String regionId = JPushInterface.getRegistrationID(this);
		if(TextUtils.isEmpty(regionId)){
			Toast.makeText(this, "初始化失败，请重新打开应用", Toast.LENGTH_SHORT).show();
			return;
		}
		ProgressUtil.showProgress(this, "");
		MyHttpUtil http = new MyHttpUtil();
		http.addBodyParm("phone", phonenum);
		http.addBodyParm("password", psw);
		http.addBodyParm("regionId", regionId);
		http.send(Url.login_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				try {
					if(object.get("status").equals("0")){
						object = object.getJSONObject("result");
						MyPreferenceManager.instance().putSharedInfoOfString(PreferenceKey.USER_INFO, object.optString("baseInfo",""));
						MyApplication.driverInfo = new Gson().fromJson(object.optString("baseInfo",""), new TypeToken<DriverInfo>() {  
	                    }.getType());
						toast("登陆成功");
						startActivity(new Intent(LoginActivity.this, MainActivity.class));
						LoginActivity.this.finish();
					}else{
						toast(object.optString("msg","数据加载出错，请重试"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
			}
		});
	}
	
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		//super.onBackPressed();
	}
	
}
